﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FF.Entity;
using FF.Method;

namespace FF
{
    public partial class FormSale : Form
    {
        public const int ACTION_TYPE_ADD = 1;
        public const int ACTION_TYPE_MODIFY_DELETE = 2;
        const string ID_HEADER = "HD-";
        private int actionType;
        //public string userId; //选择用户姓名时保存ID
        //public string userName;//选择用户时保存姓名
        private User user;
        private Order order;
        //public string customerid;
        //public string goodid;
        public FormSale(User user,string myorderid)
        {
            InitializeComponent();
            this.user = user;
            this.actionType = ACTION_TYPE_MODIFY_DELETE;
            this.order = OrderMethod.getEntityById(myorderid);
        }
        public FormSale(User user,int actiontype)
        {
            InitializeComponent();
            this.actionType = actiontype;
            this.user = user;
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            calculateTotalPrice();
        }

        private void calculateTotalPrice()
        {
            this.textBoxtotal_prices.Text = ((decimal.Parse(this.textBoxPrice.Text == "" ? "0" : this.textBoxPrice.Text)) * (decimal.Parse(this.textBoxGoodNum.Text == "" ? "0" : this.textBoxGoodNum.Text)) * (decimal.Parse(this.textBoxDiscount.Text == "" ? "0" : this.textBoxDiscount.Text))).ToString();

        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void FormSale_Load(object sender, EventArgs e)
        {
            switch (actionType)
            {
                case ACTION_TYPE_ADD:
                    //this.textBoxOrderId.Text = GoodMethod.generateID(ID_HEADER);
                    this.buttonOK.Text = "确定";
                    this.buttonDelete.Visible = false;
            	break;
                case ACTION_TYPE_MODIFY_DELETE:
                this.Text = "修改/删除销售记录";
                    this.buttonOK.Text = "修改";
                    this.buttonDelete.Visible = true;
                    this.textBoxOrderId.Text =order.orderId;
                    user.UserId = Convert.ToInt32(order.userId);
                    user.UserName = order.userName;
                     this.textBoxGoodID.Text =order.goodId ;
                     this.textBoxGoodName.Text=order.goodName ;
                      this.textBoxCustomerId.Text=order.customerId;
                     this.textBoxCustomerName.Text= order.customerName;
                     this.textBoxPrice.Text= order.goodPrice;
                      this.textBoxGoodNum.Text =order.goodNum;
                      this.textBoxtotal_prices.Text =order.totalPrices;
                      this.textBox_money_receive.Text =order.moneyReceive;
                      this.textBoxDiscount.Text =order.discount;
                     this.textBoxReceiver_name.Text= order.receiverName ;
                      this.dateTimePickerAdddate.Value = Convert.ToDateTime(order.addDate);
                      this.textBoxReceiveCompany.Text = order.receiveCompany;
                      this.richTextBoxRemark.Text = order.remark;
                break;
            }

        }

        private void textBoxPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBoxGoodNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBoxDiscount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBoxtotal_prices_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBox_money_receive_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBoxPrice_TextChanged(object sender, EventArgs e)
        {
            calculateTotalPrice();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (this.textBoxGoodNum.Text == "")
            {
                MessageBox.Show("销售数量不能为空！");
                this.textBoxGoodNum.Focus();
                return;
            }
            if (this.textBoxtotal_prices.Text == "")
            {
                MessageBox.Show("应收金额不能为空！");
                this.textBoxtotal_prices.Focus();
                return;
            }
            if (this.textBox_money_receive.Text == "")
            {
                MessageBox.Show("实收金额不能为空！");
                this.textBox_money_receive.Focus();
                return;
            }
            this.order = new Order();
            order.orderId = this.textBoxOrderId.Text;
            order.userId = user.UserId.ToString();
            order.userName = user.UserName;
            order.goodId = this.textBoxGoodID.Text;
            order.goodName = this.textBoxGoodName.Text;
            order.customerId = this.textBoxCustomerId.Text;
            order.customerName = this.textBoxCustomerName.Text;
            order.goodPrice = this.textBoxPrice.Text.Trim();
            order.goodNum = this.textBoxGoodNum.Text.Trim();
            order.totalPrices = this.textBoxtotal_prices.Text.Trim();
            order.moneyReceive = this.textBox_money_receive.Text.Trim();
            order.discount = this.textBoxDiscount.Text.Trim();
            order.receiverName = this.textBoxReceiver_name.Text.Trim();
            order.addDate = this.dateTimePickerAdddate.Value.ToString("s");
            order.receiveCompany = this.textBoxReceiveCompany.Text.Trim();
            order.remark = this.richTextBoxRemark.Text;

            //order.addYear = this.dateTimePickerAdddate.Value.Year.ToString();
            //order.addMonth = dateTimePickerAdddate.Value.Month.ToString();
            //order.addDay = dateTimePickerAdddate.Value.Day.ToString();
            switch (actionType)
            {
                case ACTION_TYPE_ADD:
                    
                    if(OrderMethod.add(order) == 1)
                    {
                        MessageBox.Show("添加成功");
                        //this.Close();
                       
                        this.textBoxGoodID.Text = "";
                        this.textBoxGoodName.Text = "";
                        this.textBoxCustomerId.Text = "";
                        this.textBoxCustomerName.Text = "";
                        this.textBoxPrice.Text = "";
                        this.textBoxGoodNum.Text = "";
                        this.textBoxtotal_prices.Text = "";
                        this.textBox_money_receive.Text = "";
                        this.textBoxDiscount.Text = "";
                        this.textBoxReceiver_name.Text = "";

                        this.textBoxReceiveCompany.Text = "";
                        this.richTextBoxRemark.Text = "";
                    }else{
                        MessageBox.Show("添加失败");


                    }
                    break;
                case ACTION_TYPE_MODIFY_DELETE:
                    if (OrderMethod.update(order) == 1)
                    {
                        MessageBox.Show("修改成功");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("修改失败");

                    }
                    break;
                   
            }

        }

        private void textBoxDiscount_TextChanged(object sender, EventArgs e)
        {
            calculateTotalPrice();
        }

        private void button_select_customer_Click(object sender, EventArgs e)
        {
            FormCustomerSelect frm = new FormCustomerSelect();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void button_select_good_Click(object sender, EventArgs e)
        {
            FormGoodSelect frm = new FormGoodSelect(FormGoodSelect.FORM_TYPE_FormSale);
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (OrderMethod.delete(this.textBoxOrderId.Text.Trim()) == 1)
                {
                    MessageBox.Show("删除成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("删除失败");

                }
            }
              
        }
    }
}
